<template>
  <div class="goods-cate">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 添加用户按钮 -->
    <el-button type="primary" plain @click="addsrot">添加分类</el-button>
    <tree-grid
      :treeStructure="true"
      :columns="columns"
      class="mt-15 mb-15"
      :data-source="dataSource"
      @deleteCate="deleteCategory"
      @editCate="editCategory"
    ></tree-grid>
    <!-- 分页器 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="1"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <!-- 添加分类对话框 -->
    <el-dialog title="添加分类" :visible.sync="addDialogFormVisible">
      <el-form :model="addForm" :rules="myrules" ref="addRef">
        <el-form-item label="分类名称" :label-width="formLabelWidth" prop="cat_name">
          <el-input v-model="addForm.cat_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="父子名称" :label-width="formLabelWidth">
          <el-cascader :options="options" v-model="selectedOptions" @change="handleChange" :props="susie" change-on-select></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="conirmAdd">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import TreeGrid from '@/components/TreeGrid/TreeGrid'
import { goodsDataList, addSort } from '@/api'
export default {
  components: {
    TreeGrid
  },
  data () {
    return {
      formLabelWidth: '100px',
      pagesize: 10,
      pagenum: 1,
      currentPage: 1,
      total: 1,
      dataSource: [],
      columns: [
        {
          text: '分类名称',
          dataIndex: 'cat_name',
          width: ''
        },
        {
          text: '是否有效',
          dataIndex: 'cat_deleted',
          width: ''
        },
        {
          text: '排序',
          dataIndex: 'cat_level',
          width: ''
        }
      ],
      addDialogFormVisible: false,
      addForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      myrules: {
        car_name: [
          {
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          }
        ]
      },
      options: [],
      selectedOptions: [], // 选中后的数据
      susie: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      }
    }
  },
  created () {
    this.initTable()
  },
  methods: {
    // 初始化列表
    initTable () {
      goodsDataList(3, this.pagesize, this.pagenum).then(res => {
        // console.log(res)
        this.total = res.data.data.total
        this.dataSource = res.data.data.result
      })
    },
    deleteCategory (cid) {
      console.log(cid)
    },
    editCategory (cid) {
      console.log(cid)
    },
    // 添加分类按钮
    addsrot () {
      this.addDialogFormVisible = true
      goodsDataList(2)
        .then(res => {
          // console.log(res)
          this.options = res.data.data
        })
      // this.$nextTick(() => {
      // // 清楚表单校验结果
      //   this.$refs.addRef.clearValidate()
      // })
    },
    handleChange (value) {
      // console.log(value)
      if (value.length === 1) {
        this.addForm.cat_level = 1
        this.addForm.cat_pid = value[0]
      } else {
        this.addForm.cat_level = 2
        this.addForm.cat_pid = value[1]
      }
    },
    // 点击确定
    conirmAdd () {
      this.$refs.addRef.validate(isPass => {
        if (isPass) {
          addSort(this.addForm)
            .then(res => {
              console.log(res)
              if (res.data.meta.status === 201) {
                console.log(res.data)
                this.addForm.cat_name = ''
                this.addForm.cat_pid = 0
                this.addForm.cat_level = 0
                this.selectedOptions = []
                this.$message.success(res.data.meta.msg)
                this.addDialogFormVisible = false
                this.initTable()
              } else {
                this.$message.error(res.data.meta.msg)
              }
            })
        } else {
          this.$message.error('请填写分类名称')
        }
      })
    },
    // 分页器
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.initTable()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.pagenum = val
      this.initTable()
    }
  }
}
</script>
